package com.zmkj.cn.comm.exception;


import com.zmkj.cn.comm.vo.CommonResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidationException;


@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public CommonResultVo handleException(Throwable e, HttpServletResponse response) {
        log.error(e.getMessage(), e);
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        if (e instanceof BindException) {
            BindException ex = (BindException) e;
            ObjectError error = ex.getAllErrors().get(0);
            log.error("数据验证异常：{}", error.getDefaultMessage());
            return CommonResultVo.validateFailed(error.getDefaultMessage());
        }
        return CommonResultVo.failed(e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public CommonResultVo globle(Exception e) {
        e.printStackTrace();
        log.error("错误异常信息：" + e.getMessage());
        if (e instanceof BaseException) {
            return CommonResultVo.unauthorized(e.getMessage());
        } else if (e instanceof RuntimeException) {
            RuntimeException ex = (RuntimeException) e;
            return CommonResultVo.validateFailed(ex.getMessage());
        } else if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            ObjectError error = ex.getBindingResult().getAllErrors().get(0);
            log.error("数据验证异常：{}", error.getDefaultMessage());
            return CommonResultVo.validateFailed(error.getDefaultMessage());
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            ObjectError error = ex.getAllErrors().get(0);
            log.error("数据验证异常：{}", error.getDefaultMessage());
            return CommonResultVo.validateFailed(error.getDefaultMessage());
        } else if (e instanceof ValidationException) {
            ValidationException ex = (ValidationException) e;
            log.error("数据验证异常：{}", ex);
            return CommonResultVo.validateFailed(ex.getMessage());
        } else {
            return CommonResultVo.failed(e.getMessage());
        }
    }
}
